package com.heima.item.controller;
import com.heima.common.Result;
import com.heima.dto.DeductStockDTO;
import com.heima.entity.Item;
import com.heima.item.service.IItemService;
import com.heima.po.ItemDoc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author zhuan
 * @since 2023-09-06
 */
@RestController
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @GetMapping("/page/{page}/{size}")
    public Result page(@PathVariable(value = "page",required = false)int page,
                       @PathVariable(value = "size",required = false) int size){
        List<ItemDoc> list = itemService.page(page, size);
        return new Result(200,"查询商品成功",true,list);
    }
    /**
     * 功能描述: 批量扣减库存
     * @param dtos
     *  id  商品ID
     *  num 扣减数量
     * @return : com.heima.common.Result
     */
    @PostMapping("/deductStock")
    public Result deductStock(@RequestBody List<DeductStockDTO> dtos){
        itemService.deductStock(dtos);
        return new Result(200,"查询商品成功",true);
    }

    @PostMapping("/save")
    public Result saveItem(@RequestBody Item item){
        Boolean flag = itemService.saveItem(item);
        if(flag){
            return new Result(200,"新增商品成功",true);
        }
        return new Result(500,"新增商品失败",false);
    }
}
